package com.eclink.iot.tcpserver.websocket;

import com.eclink.iot.rfid.RFIDServer;
import com.eclink.iot.tcpserver.entity.WelcomeMessage;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Component
@ServerEndpoint("/device")
public class WebSocketServer {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connection opened: " + session.getId());
        sessions.add(session);
    }

    @OnMessage
    public void onMessage(Session session, String message) throws Exception {

        System.out.println("Received message: " + message);
        session.getBasicRemote().sendText(message);
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("Connection closed: " + session.getId());
        sessions.remove(session);
    }

    public static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<>());
}
